package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.vo.GoodsVO;
import com.dongdongshop.service.goods.GoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/3/2 17:59
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    @Reference
    private GoodsService goodsService;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    Logger log = LoggerFactory.getLogger(GoodsController.class);

    /**
     * 跳转商品审核页
     * @return
     */
    @RequestMapping("toExamineGoods")
    public String toExamineGoods(Model model){
        List<GoodsVO> list = goodsService.getZeroStatusGoodsVO();
        model.addAttribute("goods",list);
        return "goods/goodsExamine";
    }

    /**
     * 审核商品 status=1审核通过 status=2驳回
     */
    @RequestMapping(value = "updateGoodsStatusById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateGoodsStatusById(@RequestParam("ids[]")Long[] ids,String status){
        try {
            goodsService.updateBatchGoodsStatusById(ids,status);
            //发送mq消息给商品服务的生成商品详情静态页方法
            if(status.equals("1")){
                for (Long id : ids) {
                    //freemarker-topic:消息主题     id:发送的消息
                    rocketMQTemplate.convertAndSend("freemarker-topic",id);
                }
            }
        }catch (Exception e){
            log.error("审核商品异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }


}
